package org.sy.lab.多线程基础._4_执行器._1_创建线程池._4_newScheduledThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.sy.lab.多线程基础._4_执行器._1_创建线程池._1_newFixedThreadPool.MyThread;

//创建一个定长线程池，支持定时及周期性任务执行
public class Main {
	
	public static void main(String[] args) {
		
		ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(3);
		
		for (int i = 0; i < 5; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.schedule(myThread, 10, TimeUnit.SECONDS);
		}
		
		for (int i = 5; i < 10; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		
		for (int i = 10; i < 15; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		executor.shutdown();
		try {
			executor.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
	}
}
